{createAction} = require('redux-actions')
Toast = require('antd-mobile/lib/toast')
actions =
  getListStart:createAction('PILOT_INFO/GET_LIST_START')
  getListCompleted:createAction('PILOT_INFO/GET_LIST_COMPLETED')
  refreshListStart:createAction('PILOT_INFO/REFRESH_LIST_START')
  refreshListCompleted:createAction('PILOT_INFO/REFRESH_LIST_COMPLETED')
  updatefail:createAction('PILOT_INFO/UPDATE_FAIL')
  updateCompleted:createAction('PILOT_INFO/UPDATE_COMPLETED')
  fetchUpdate:(info,token)->
    debugger
    (dispatch)->
      fetch("http://202.136.214.173/api/PilotInfo",{
          method: 'POST'
          mode: 'cors'
          cache: 'default'
          headers: {
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          }
          body:"shr=#{info.shr}&shdz=#{info.shdz}&shdh=#{info.shdh}"
          # body:JSON.stringify(info)

        })
      .then (res)->
        debugger
        if res.ok and res.status is 200
          res.json()
      .then (json)->
        # dispatch actions.pilotConfirmCompleted
        #   planid:planid
        Toast.success("地址修改成功")
      .catch (err)->
        # dispatch actions.pilotConfirmfail(err)
        Toast.fail('地址修改失败，请稍后重试')
  fetchGetList:(token)->
    (dispatch)->
      debugger
      dispatch actions.getListStart()
      fetch("http://202.136.214.173/api/PilotInfo",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers:
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        debugger
        res.json()
      .then (json)->
        #获取当前时间
        dispatch actions.getListCompleted
          pilotInfo:json
          lastFetchTime:new Date()
      .catch (err)->
        debugger
        console.log(err)
  fetchRefreshList:(token)->
    (dispatch)->
      dispatch actions.refreshListStart()
      fetch("http://202.136.214.173/api/PilotInfo",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers:
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        debugger
        res.json()
      .then (json)->
        debugger
        dispatch actions.refreshListCompleted
          pilotInfo:json
          lastFetchTime:new Date()
      .catch (err)->
        debugger
        console.log(err)

module.exports = actions
